#include "sim_driver.h"
#include "cs104_server.h"
#include <QTimer>

simulator_driver::simulator_driver(const char* addr, int port):m_pServer(new simple_server(addr, port))
{
	m_pServer->initHandle();

	std::vector<S_Yc> vt_yc;

	for (int i = 0; i < 100; i++)
	{
		S_Yc yc;
		yc.addr = 0x4001 + i;
		yc.value = i;
		yc.qc = 0;

		vt_yc.push_back(yc);
	}

	m_pServer->setYC(vt_yc);

	QTimer* timer = new QTimer;
	QObject::connect(timer, &QTimer::timeout, [&]() {
		if (m_pServer->isActivated())
		{
			printf("send YC\n");
			m_pServer->sendYC(0x4001, 14.5, 0);
			m_pServer->sendYXSingle(0x01, 1, 0);
			m_pServer->sendYXDouble(0x02, 2, 0);
			m_pServer->sendYXSingle(0x03, 1, 0);
		}
			
		});
	timer->start(1000);
}

simulator_driver::~simulator_driver()
{
	m_pServer->stop();

	if (m_pServer != NULL)
	{
		delete m_pServer;
		m_pServer = NULL;
	}
}

void simulator_driver::run()
{
	m_pServer->start();

	
}